package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.bean.OrderDetailBean;
import com.dlwx.firefighting.bean.OrderNowBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.inter.PayPswInter;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.PayPswDialog;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PayActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.phone_tv)
    TextView phoneTv;
    @BindView(R.id.address_tv)
    TextView addressTv;
    @BindView(R.id.recycler)
    LRecyclerView recycler;
    @BindView(R.id.order_num_tv)
    TextView orderNumTv;
    @BindView(R.id.goods_total_money_tv)
    TextView goodsTotalMoneyTv;
    @BindView(R.id.goods_money_tv)
    TextView goodsMoneyTv;
    @BindView(R.id.express_money_tv)
    TextView expressMoneyTv;
    @BindView(R.id.money_tv)
    TextView moneyTv;
    @BindView(R.id.order_details_cancel)
    TextView orderDetailsCancel;
    @BindView(R.id.order_details_fukuan)
    TextView btnFukuan;
    @BindView(R.id.order_details_rel)
    AutoLinearLayout orderDetailsRel;
    String orderId = "";
    @BindView(R.id.balance_rb)
    RadioButton balanceRb;
    @BindView(R.id.integral_rb)
    RadioButton integralRb;
    @BindView(R.id.goods_img)
    ImageView goodsImg;
    @BindView(R.id.goods_title_tv)
    TextView goodsTitleTv;
    @BindView(R.id.goods_integral)
    TextView goodsIntegral;
    @BindView(R.id.goods_money_ll)
    RelativeLayout goodsMoneyLl;
    @BindView(R.id.tag)
    TextView tag;
    @BindView(R.id.num_tv)
    TextView numTv;
    @BindView(R.id.sales_volume)
    TextView salesVolume;
    @BindView(R.id.goods_money_al)
    AutoRelativeLayout goodsMoneyAl;
    @BindView(R.id.remark_tv)
    TextView remarkTv;
    @BindView(R.id.money_ll)
    LinearLayout moneyLl;
    @BindView(R.id.address_al)
    AutoRelativeLayout addressAl;
    private String userId;
    private String goodsId;
    private String addressId;
    private float total;
    private String shippingName;
    private String shippingFee;
    private String payName;
    private String goodsNum;
    private int payType = Constant.PAY_MONEY;
//    是否用余额支付
    private boolean isPayByMoney = true;
//    收货地址是否存在
    private boolean isAddressExist = true;
    private String integralParams = "";
    private boolean isPay=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_now);
        ButterKnife.bind(this);
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        orderId = getIntent().getStringExtra("id");
        OrderNowBean bean = (OrderNowBean) getIntent().getSerializableExtra("bean");
        initView();
        setDatas(bean);
    }

    @Override
    protected void onResume() {
        super.onResume();
        isPay= (boolean) SPUtils.get(this,Constant.IS_PAY,false);
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("支付");
    }


    private void setDatas(OrderNowBean bean) {
        OrderNowBean.AddressBean address = bean.getAddress();
        addressId = bean.getAddress().getAddress_id();
        if(TextUtils.isEmpty(address.getAddress())){
            addressTv.setText("请先添加收货地址！");
            ToastUtils.toastForShort(PayActivity.this,"请先添加收货地址！");
            isAddressExist=false;
        }else{
            nameTv.setText("收货人：" + bean.getAddress().getConsignee());
            addressTv.setText("收货地址：" + address.getAddress());
            isAddressExist=true;
        }
//        addressTv.setText("收货地址：" +address.getCity()+address.getDistrict()+address.getAddress());
        phoneTv.setText(address.getMobile());
       /* expressTypeTv.setText("配送方式：" + "顺丰速运");
        expressTimeTv.setText("配送时间：");*/
        goodsTotalMoneyTv.setText(getString(R.string.rmb) + bean.getGoods_total());
        expressMoneyTv.setText(getString(R.string.rmb) + bean.getShipping_free());
        moneyTv.setText(getString(R.string.rmb) + bean.getTotal());
        btnFukuan.setText("付款");
//
        List<OrderDetailBean.XiangBean> goodsLists = bean.getGoods();
        if (!goodsLists.isEmpty()) {
            OrderDetailBean.XiangBean goodsBean = goodsLists.get(0);
            Glide.with(this).load(goodsBean.getGoods_thumb()).error(R.mipmap.error_small).into(goodsImg);
            final double maxIntegral = goodsBean.getMax_integral();
            goodsIntegral.setText(goodsBean.getMax_integral() + " 积分");
            goodsMoneyTv.setText(getString(R.string.rmb) + bean.getGoods_total());
            goodsTitleTv.setText(goodsBean.getGoods_name());
//            integralTv.setText(""+bean.getIntegral());
            numTv.setText(getString(R.string.x) + bean.getNum());
            goodsId = goodsBean.getGoods_id();
        }
//
        goodsNum = bean.getNum();
        shippingFee = bean.getShipping_free();
        float f = 0;
        try {
            f = Float.parseFloat(bean.getTotal() + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        total = f;
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title, R.id.order_details_fukuan, R.id.balance_rb
            , R.id.integral_rb, R.id.address_al})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
            case R.id.order_details_fukuan:
//                integralParams = integralEt.getText().toString().trim();
//                if(!isAddressExist){
//                    ToastUtils.toastForShort(PayActivity.this,"请先添加收货地址！");
//                    return;
//                }
//                doPayPsw();
////                doPay();
                break;
            case R.id.balance_rb:
                isPayByMoney = true;
                setPayType(isPayByMoney);
                payType = Constant.PAY_MONEY;
                break;
            case R.id.integral_rb:
                isPayByMoney = false;
                setPayType(isPayByMoney);
                payType = Constant.PAY_INTEGRAL;
                break;
            case R.id.address_al:
                PayActivity.this.finish();
               startActivity(new Intent(PayActivity.this,AddressListActivity.class));
                break;
        }
    }

    private void doPayPsw() {
        if(!isPay){
            final NormalDialog dialog = DialogUtils.getNormalDialog(this, "提示", "设置支付密码");
            dialog.setOnBtnClickL(new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    dialog.dismiss();
                }
            }, new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    dialog.dismiss();
                    startActivity(new Intent(PayActivity.this,PayPswActivity.class));
                }
            });
            return;
        }
        PayPswDialog mDialog = new PayPswDialog(this, new PayPswInter() {
            @Override
            public void getPsw(String psw) {
                doPay(psw);
            }
        });
        mDialog.show();
    }

    private void setPayType(boolean isPayByMoney) {
        balanceRb.setChecked(isPayByMoney);
//        if (isPayByMoney) {
//            integralRb.setChecked(false);
//            integralEt.setVisibility(View.INVISIBLE);
//            moneyLl.setVisibility(View.VISIBLE);
//            remarkTv.setVisibility(View.GONE);
//        } else {
//            integralRb.setChecked(true);
//            integralEt.setVisibility(View.VISIBLE);
//            moneyLl.setVisibility(View.GONE);
//            remarkTv.setVisibility(View.VISIBLE);
//        }
    }

    private void doPay(String psw) {
        DialogUtils.showProgressDlg(this,"");
        OkGo.<String>post(Constant.insertOrderInfo)
                .tag(this)
                .params("user_id", userId)
                .params("address_id", addressId)
                .params("total", total)
                .params("goods_id", goodsId)
                .params("goods_number", goodsNum)
                .params("integral", integralParams)
                .params("pay_id", payType)
                .params("pay_password", psw)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        DealResult dealResult = new DealResult();
//                        BasicBean basicBean=dealResult.dealBasic(PayActivity.this,response);
//                        if (basicBean == null) {
//                            return;
//                        }
//                        PayResBean bean = dealResult.dealOne(PayActivity.this, response, new TypeToken<BaseOneBean<PayResBean>>() {
//                        }.getType());
//                        if (bean == null) {
//                            return;
//                        }
//                        ToastUtils.toastForShort(PayActivity.this, "支付成功");
//                        integralParams = "";
//                        Intent i = new Intent(PayActivity.this, PayResultActivity.class);
//                        i.putExtra("bean", bean);
//                        startActivity(i);
//                        PayActivity.this.finish();
                    }
                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(PayActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });

    }
}
